<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table
      v-loading="table.loading"
      :data="table.data"
      row-key="id"
      lazy
      :load="loadDeptTree"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="组织名称" min-width="400"></el-table-column>
      <el-table-column prop="code" label="组织编码" align="center" width="180"></el-table-column>
      <el-table-column prop="fzrName" label="负责人" align="center" width="200"></el-table-column>
      <el-table-column prop="fgName" label="分管领导" align="center" width="200"></el-table-column>
      <el-table-column label="状态" align="center" key="status" width="180">
        <template slot-scope="scope">
          <status-tag :status-options="statusOptions" :status="scope.row.status"></status-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listDept } from "@/api/system/dept";

export default {
  name: "Dept",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      statusOptions: [],
      // 查询参数
      queryParams: {
        deptName: undefined
      },
      table: {
        data: [],
        loading: false,
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.initData();
      this.getDicts("sys_normal_disable").then(res => {
        this.statusOptions = res;
      })
    },
    /** 查询部门列表 */
    initData() {
      this.table.loading = true;
      listDept({
        page: false,
        parentId: this.queryParams.deptName ? null : "0",
        status: "0",
        deptName: this.queryParams.deptName
      }).then(response => {
        this.$lodash.each(response, item => {
          item.hasChildren = !item.leaf;
        });
        this.table.data = response;
        this.table.loading = false;
      }).catch(() => {
        this.table.loading = false;
      });
    },
    loadDeptTree(tree, treeNode, resolve) {
      listDept({
        page: false,
        parentId: tree.id,
        status: "0"
      }).then(res => {
        this.$lodash.each(res, item => {
          item.hasChildren = !item.leaf;
        });
        resolve(res);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
